package com.info.zhiduoduo.admin.service;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.admin.model.formbean.foreign.ChForeignProjectFromBean;
import com.info.zhiduoduo.admin.model.formbean.foreign.ForeignProjectFromBean;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.RandomUtil;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.foreign.ExportForeignProjectDto;
import com.info.zhiduoduo.repository.dto.foreign.ForeignProjectDraftListDto;
import com.info.zhiduoduo.repository.dto.foreign.ForeignProjectDto;
import com.info.zhiduoduo.repository.mapper.foreign.ForeignProjectDraftMapper;
import com.info.zhiduoduo.repository.mapper.foreign.ForeignProjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ForeignProjectService extends BaseService {

    @Resource
    ForeignProjectMapper foreignProjectMapper;

    @Resource
    ForeignProjectDraftMapper foreignProjectDraftMapper;

    /**
     * 项目列表
     * @param formBean
     * @return
     */
    public SmartResult<ForeignProjectDto> projectList(@RequestBody BaseSimpleFormBean<ForeignProjectFromBean> formBean){

        ForeignProjectFromBean reqdata = formBean.getReqdata();
        String name = reqdata.getName();
        Date startDate = reqdata.getStartDate();
        Date endDate = reqdata.getEndDate();
        if (startDate == null) {
            startDate = new Date(0);
        }
        if (endDate == null) {
            endDate = new Date();
        }
        Page<Object> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        List<ForeignProjectDto> foreignProjectDtoList = foreignProjectMapper.projectList(name,startDate,endDate);
        return smartResult(foreignProjectDtoList, page.getTotal(), page.getPages(), page.getPageNum());
    }

    /**
     * 导出第三方项目列表列表
     * @param response
     */
    public void exportForeignProjectList(HttpServletResponse response, ForeignProjectFromBean reqdata) {

        String name = reqdata.getName();
        Date startDate = reqdata.getStartDate();
        Date endDate = reqdata.getEndDate();
        if (startDate == null) {
            startDate = new Date(0);
        }
        if (endDate == null) {
            endDate = new Date();
        }

        //开票中,或已开票
        List<ExportForeignProjectDto> list = foreignProjectMapper.exportForeignProjectList(name,startDate,endDate);

//        EasyExcelUtil.download(response, "嘉银任务信息", ExportForeignProjectDto.class, list);
        String file = RandomUtil.createRandomNo( "嘉银任务信息", "");
        try {
            EasyExcel.write(getExcelOutPutStream(response, file), ExportForeignProjectDto.class).sheet("嘉银任务信息").doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public SmartResult queryCHProjectDraftList(BaseSimpleFormBean<ChForeignProjectFromBean> formBean){
        if(null == formBean || null == formBean.getReqdata()){
            return errorParamSmartResult();
        }
        ChForeignProjectFromBean bean = formBean.getReqdata();
        Date startOrderTime = bean.getStartOrderTime();
        Date endOrderTime = bean.getEndOrderTime();
        Date startCreateTime = bean.getStartCreateTime();
        Date endCreateTime = bean.getEndCreateTime();
        if (startOrderTime == null) {
            startOrderTime = new Date(0);
        }
        if (endOrderTime == null) {
            endOrderTime = new Date();
        }
        if (startCreateTime == null) {
            startCreateTime = new Date(0);
        }
        if (endCreateTime == null) {
            endCreateTime = new Date();
        }
        String number = bean.getNumber();
        String name = bean.getName();
        Page<Object> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        List<ForeignProjectDraftListDto> draftListDtoList = foreignProjectDraftMapper.queryForeignProjectDraftByCondition(startOrderTime,endOrderTime,startCreateTime,endCreateTime,number,name);
        return smartResult(draftListDtoList, page.getTotal(), page.getPages(), page.getPageNum());
    }
}
